/**
 * 
 */
package team3;

/**
 * @author dzsebedi
 * 
 */
public class Parser {

	Tripel tripel = null;
	Scanner scanner = new Scanner();

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		Parser parser = new Parser();
		parser.start();

	}
	
	public void start() {
	
		scanner.scanneAusdruck("2^3^2;");
		tripel = scanner.naechstesZeichen();
		System.out.println(ausdruck());
		if (tripel == null || tripel != null && !tripel.getToken().equals(Token.ENDE.name())) {
			System.out.println("ERROR");;
		}
	}

	public double ausdruck() {
		
		double sa1, sa2, sa3, sa4, ha2, ha3, ha4, ha6, ha7, ha8;
		char ha1, ha5;

		if (tripel.getToken().equals(Token.MINUS.name())) {
			tripel = scanner.naechstesZeichen();
			sa2 = malDurchTerm();
			ha1 = '-'; ha2 = sa2; ha3 = 0.0;
			ha4 = - ha2;
			ha6 = ha4;
			out(ha1, ha2, ha3, ha4);
		} else {
			sa3 = malDurchTerm();
			ha6 = sa3;
		}
		while (tripel != null && tripel.getToken().equals(Token.MINUS.name())
				|| tripel != null && tripel.getToken().equals(Token.PLUS.name())) {
			ha5 = tripel.getLexem().charAt(0);
			tripel = scanner.naechstesZeichen();
			sa4 = malDurchTerm();
			ha7 = sa4;
			ha8 = ha5 == '+' ? ha6 + ha7 : ha6 - ha7;
			out(ha5, ha6, ha7, ha8);
			ha6 = ha8;
		}
		sa1 = ha6;
		
		return sa1;
	}

	public double malDurchTerm() {
		
		double sa1, sa2, sa3, ha2, ha3, ha4;
		char ha1;
		
		sa2 = potTerm();
		ha2 = sa2;
		while (tripel != null && tripel.getToken().equals(Token.MAL.name())
				|| tripel!= null && tripel.getToken().equals(Token.DURCH.name())) {
			ha1 = tripel.getLexem().charAt(0);
			tripel = scanner.naechstesZeichen();
			sa3 = potTerm();
			ha3 = sa3;
			ha4 = ha1 == '*' ? ha2 * ha3 : ha2 / ha3;
			ha2 = ha4;
		}
		
		sa1 = ha2;
		
		return sa1;

	}

	public double potTerm() {
		
		double sa1, sa2, sa3, ha2, ha3, ha4;
		char ha1;
		
		sa2 = faktor();
		ha2 = sa2;
		if (tripel != null && tripel.getToken().equals(Token.HOCH.name())) {
			ha1 = '^';
			tripel = scanner.naechstesZeichen();
			sa3 = potTerm();
			ha3 = sa3;
			ha4 = Math.pow(ha2, ha3);
			ha2 = ha4;
		}
		
		sa1 = ha2;
		return sa1;
	}

	public double faktor() {
		
		double sa1, sa2, ha6;
		ha6 = 0.0;
		
		if (tripel != null && tripel.getToken().equals(Token.KLAMMERAUF.name())) {
			tripel = scanner.naechstesZeichen();
			sa2 = ausdruck();
			ha6 = sa2;
			if (tripel != null && tripel.getToken().equals(Token.KLAMMERZU.name())) {
				tripel = scanner.naechstesZeichen();
			} else {
				System.out.println("ERROR");
			}
		} else if (tripel != null && tripel.getToken().equals(Token.ZAHL.name())) {
			ha6 = Double.parseDouble(tripel.getLexem());
			tripel = scanner.naechstesZeichen();
		} else {
			System.out.println("ERROR");
		}
		sa1 = ha6;
		
		return sa1;
	}
	
	public void out(char a, double b, double c, double d) {
		System.out.println("Quadrupel: (" + a +", " + b + ", " + c + ", " + d + ")");
	}
}
